package com.yonyou.crm.sprc.oppaction.ctrl;

import com.yonyou.crm.base.doc.entity.BaseDocDetailVO;
import com.yonyou.crm.base.doc.entity.BaseDocVO;
import com.yonyou.crm.base.doc.rmitf.IBaseDocRmService;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.HttpKeyConstant;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sprc.oppaction.entity.OppactionVO;
import com.yonyou.crm.sprc.oppaction.rmitf.IOppactionRmService;
import com.yonyou.crm.sprc.oppdimension.entity.OppdimensionVO;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/sprc/oppactions")
public class OppactionCtrl {

	@Autowired
	private IOppactionRmService service;

	@Autowired
	private IBaseDocRmService baseDocRmService;
	/**
	 * 根据ID数组返回档案信息和档案详情
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "docs", method = RequestMethod.GET)
	public Object getDocAndDetailList(HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("9","dimension");
		Map baseDocList = baseDocRmService.getDocAndDetailList(map);
		JSONObject result = new JSONObject();
		result.put("enumData", baseDocList);
		return ResponseUtil.getObject(result);
	}

	/**
	 * 商机动作列表
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "dimension",method = RequestMethod.GET)
	public Object getByDimension(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);
//		OppdimensionVO vo = RequestUtil.getVO(paramObj.getJSONObject("dimension"), OppdimensionVO.class);
		@SuppressWarnings("unchecked")
		List<Map<String, Object>> dimensionList = (List<Map<String, Object>>)paramObj.get("dimension");
		List dimIds = new ArrayList();
		for(int i=0;i<dimensionList.size();i++){
			dimIds.add(dimensionList.get(i).get("key"));
		}
		List<OppactionVO> list = service.getByDimension(dimIds);
		JSONObject result = new JSONObject();
		result.put("action", list);
		return ResponseUtil.getObject(result);

	}


	/**
	 * 商机动作列表
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object list(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);

		@SuppressWarnings("unchecked")
		Page<OppactionVO> page = service.getList(getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));

		return ResponseUtil.getObject(page);

	}

	/**
	 * 新增商机动作
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Object insert(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		OppactionVO vo = RequestUtil.getVO(request, OppactionVO.class);

		String validMsg = validator(vo);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}

		vo = service.insert(vo);

		if (vo.getId() == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "获取自增主键为空");
		}

		return ResponseUtil.getObject(vo,"插入成功");
	}

	/**
	 * 修改商机动作
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		OppactionVO vo = RequestUtil.getVO(request, OppactionVO.class);
		if (vo == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "转换实体失败");
		}

		vo.setId(id);

		String validMsg = validator(vo);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}

		return ResponseUtil.getObject(service.update(vo),"修改成功");
	}


	/**
	 * 批量删除
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "batch", method = RequestMethod.DELETE)
	public Object batchDelete(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}

		String[] idArray = idsStr.split(",");

		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}


		@SuppressWarnings("unchecked")
		Page<OppactionVO> page = service.batchDelete(idArray,getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));

		return ResponseUtil.getObject(page,"删除成功");
	}

	/**
	 * 批量停启用
	 * @param request
	 * @param response
	 * @return
	 */

	@RequestMapping(value = "state", method = RequestMethod.PUT)
	public Object batchUpdateEnableState(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}

		String[] idArray = idsStr.split(",");

		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}

		Integer enableState = paramObj.getInt("enableState");

		if (enableState == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "停启用状态不能为空");
		}

		@SuppressWarnings("unchecked")
		Page<OppactionVO> page = service.batchUpdateEnableState(idArray, enableState,getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));
		String msg="停用成功";
		if("1".equals(enableState.toString())){
			msg="启用成功";
		}
		return ResponseUtil.getObject(page,msg);
	}


	private String validator(OppactionVO vo) {

		if (vo == null) {
			return "转换实体失败";
		}

		if (StringUtils.isBlank(vo.getName())) {
        	return  "名称不能为空";
		}

		return "";
	}
	/**
	 * 获取分页信息
	 * @param paramObj
	 * @return Page<MeasuresVO>
	 */
	private Page<OppactionVO> getRequestPage(JSONObject paramObj) throws CrmMessageException {

		Integer page = paramObj.getInt(HttpKeyConstant.Page);
		Integer pageSize = paramObj.getInt(HttpKeyConstant.PageSize);

		if (page == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "page不能为空");
		}

		if (pageSize == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "pageSize不能为空");
		}

		return new Page<OppactionVO>(page, pageSize);
	}
}
